package com.nf.health.app.activity.my.adapter;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.myfamily.activity.FamilyMemberActivity;
import com.nf.health.app.activity.my.impl.MyContact;
import com.nf.health.app.customview.CircleImageView;
import com.nf.health.app.glide.GlideUtil;
import com.nf.health.app.models.Family;
import com.nf.health.app.models.HouseResponblePresion;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ViewUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/2/17/017.
 */

public class GridHouseResponblePresionAdapter   extends BaseAdapter {
    List<HouseResponblePresion> list;
    Activity activity;
    LayoutInflater inflater = null;
    public GridHouseResponblePresionAdapter(Activity activity, List<HouseResponblePresion> list) {
        this.activity = activity;
        this.list = list;
        inflater = LayoutInflater.from(activity);
    }
    public void setMyNOtifuDataChanged(List<HouseResponblePresion> list) {
        this.list = list;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        if (list != null && list.size() != 0) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder = null;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.item_grid_responbility_persion,null);
            myViewHolder = new MyViewHolder();
            myViewHolder.civ_house_img = ViewUtil.getView(convertView,R.id.civ_house_img);
            myViewHolder.tv_housr_respername = ViewUtil.getView(convertView,R.id.tv_housr_respername);
            myViewHolder.ll_housr_respername = ViewUtil.getView(convertView,R.id.ll_housr_respername);
            convertView.setTag(myViewHolder);
        }else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }
        final HouseResponblePresion houseResponblePresion  = list.get(position);
        GlideUtil.loadNetImgC(activity,myViewHolder.civ_house_img,houseResponblePresion.getHimg(),R.drawable.default_head);
        myViewHolder.tv_housr_respername.setText( houseResponblePresion.getRname() == null ? houseResponblePresion.getNick() : houseResponblePresion.getRname());
        myViewHolder.ll_housr_respername.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                Family family = new Family();
                family.setType("");
                family.setScore("");
                family.setAccount(houseResponblePresion.getNick());
                String name = houseResponblePresion.getRname();
                if(!TextUtils.isEmpty(name)){
                    family.setNick(name);
                }else if(!TextUtils.isEmpty(houseResponblePresion.getNick())){
                    family.setNick(houseResponblePresion.getNick());
                }else{
                    family.setNick("");
                }
                family.setTouserid(houseResponblePresion.getUserid());
                bundle.putSerializable("member", family);
                bundle.putString("Score", "gone");
                bundle.putString(MyContact.FLAG_HELATHY_MANAGER_PROGECT, MyContact.FLAG_HELATHY_MANAGER_PROGECT);
                ActivityUtils.showActivity(activity, FamilyMemberActivity.class, bundle);
            }
        });
        return convertView;
    }
    public class MyViewHolder {


        /// @Bind(R.id.civ_house_img)
        CircleImageView civ_house_img;
        //  @Bind(R.id.tv_housr_respername)
        TextView tv_housr_respername;
        //  @Bind(R.id.ll_housr_respername)
        LinearLayout ll_housr_respername;
    }
}